﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

namespace Sharp.WebBooter
{
    public class mxGraphHttpModule : IHttpModule
    {
        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            context.AuthenticateRequest += Context_AuthenticateRequest;
        }

        private static readonly Regex Rules = new Regex("WorkFlow/(Process)(.*?)/(config|diagrams|images|resources)", RegexOptions.Compiled | RegexOptions.IgnoreCase);



        private void Context_AuthenticateRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            HttpContext context = app.Context;
            string path = context.Request.Path;
            if (!Rules.IsMatch(path))
            {
                return;
            }

            var mxgraph = Regex.Replace(path, "(.*?)/(config|diagrams|images|resources)/", "~/Scripts/mxgraph/$2/");
            //mxgraph = path.Replace("/Platform/WorkFlow", "~/Scripts/mxgraph/");
            context.RewritePath(mxgraph);

        }
    }
}